/* Copyright 2009 Sony Mathew, Minneapolis MN. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
package com.xsm.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * ONLY to be used for debugging use in DEV.
 * 
 * @author Sony Mathew
 */
public class PingServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final HttpSession session = req.getSession(); 
        boolean isNewSession = session.isNew();        
        String sessionSource = req.isRequestedSessionIdFromCookie()? "Cookie" : (req.isRequestedSessionIdFromURL()? "URL" : "Unknown");
        String app = req.getContextPath();
        int pingCount = incPingCount(req);
        String sessionId = session.getId();
        String pingUrl = createPingUrl(req, resp); 
        
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache, no-store");
        resp.setHeader("Expires", "-1");
        resp.setContentType("text/html");
        resp.getWriter().println("<h3>Application=[" + app + "]</h3>");
        resp.getWriter().println("<h3>Session ID=[" + sessionId + "]</h3>");
        resp.getWriter().println("<h3>Session ID Encoded=[" + resp.encodeURL(sessionId) + "]</h3>");
        resp.getWriter().println("<h3>Session isNew=[" + isNewSession + "]</h3>");
        resp.getWriter().println("<h3>Session Source=[" + sessionSource + "]</h3>");
        resp.getWriter().println("<h3>Ping Count this Session=[" + pingCount + "]</h3>");
        resp.getWriter().println("<A href=" + pingUrl + ">Ping Again</A>");
        
    }

    
    private int incPingCount(HttpServletRequest req) {
        int pingCount = 0;        
        final HttpSession session = req.getSession();
        final String pingKey = getClass().getName()+".ping.count";
        String pingCountStr = (String)session.getAttribute(pingKey);        
        if (pingCountStr != null) {
            pingCount = Integer.parseInt(pingCountStr); 
        }
        pingCount++;
        session.setAttribute(pingKey, String.valueOf(pingCount));
        return pingCount;
    }
    
    private String createPingUrl(HttpServletRequest req, HttpServletResponse resp) {
        return resp.encodeURL(req.getRequestURL().toString());
    }
}
